//
//  StatusCell.swift
//  Weibo
//
//  Created by 张远文 on 16/4/30.
//  Copyright © 2016年 张远文. All rights reserved.
//

import UIKit
import SnapKit

let statusCellMargin: CGFloat = 12
let statusCellIconWidth: CGFloat = 35

class StatusCell: UITableViewCell {

    //原创微博底部约束  用来记录底部底部约束,后续根据是否有配图,动态修改
    var bottomConstraint: Constraint?
    
    //数据模型
    var status: Status? {
        
        didSet {
            originalView.status = status
            
            //移除约束条件
            self.bottomConstraint?.uninstall()
            
            //给配图设置数据源
            if let picurls = status?.retweeted_status {
                //设置数据源
                retweetedView.retweetedStatus = picurls
                //设置显示
                retweetedView.hidden = false
                bottomView.snp_updateConstraints(closure: { (make) in
                    
                    self.bottomConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint
                })

            } else {
                //设置隐藏
                retweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
        }
    }
    
    //重写构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setupUI() {
        //添加子视图
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(bottomView)
        
        //设置约束
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
        }
        
        retweetedView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
        }
        
        bottomView.snp_makeConstraints { (make) in
            make.left.equalTo(contentView.snp_left)
            self.bottomConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
        }
        //容器视图的底部约束 自动计算行高
        contentView.snp_makeConstraints { (make) in
            make.bottom.equalTo(bottomView.snp_bottom)
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
        }
    }
    
//    override func awakeFromNib() {
//        super.awakeFromNib()
//        // Initialization code
//    }
//
//    override func setSelected(selected: Bool, animated: Bool) {
//        super.setSelected(selected, animated: animated)
//
//        // Configure the view for the selected state
//    }
    
    //MARK: - 懒加载控件
    private lazy var originalView: StatusOriginalView = StatusOriginalView()
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    private lazy var retweetedView: ReweetedStatusView = ReweetedStatusView()

}
